<template>
  <div class="dialog">
    <el-dialog :title="'数据项' + titleText" :visible.sync="showDialog" width="35%" :close-on-click-modal="false"
      @close="cancel">
      <el-form ref="ruleForm" :model="formData" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="数据项名称 :" prop="dataItemName">
          <el-input v-model="formData.dataItemName" placeholder="请输入数据项名称" clearable />
        </el-form-item>
        <el-form-item label="数据类型 :" prop="dataTypeId">
          <el-select class="w100p" filterable clearable v-model="formData.dataTypeId" placeholder="选择数据类型"
            @change="changeDataType">
            <el-option v-for="(item, index) of dataTypeList" :key="item.dataTypeId + 'dataType' + index"
              :label="item.dataTypeName" :value="item.dataTypeId" />
          </el-select>
        </el-form-item>
        <el-form-item label="绑定设备:" prop="equipmentId">
          <el-select class="w100p" filterable clearable v-model="formData.equipmentId" placeholder="请选择设备">
            <el-option v-for="item in equipmentlist" :key="item.equipmentId" :label="item.equipmentName"
              :value="item.equipmentId" />
          </el-select>
        </el-form-item>
        <el-form-item label="是否可展示:" prop="visible">
          <el-select class="w100p" filterable clearable v-model="formData.visible" placeholder="请选择">
            <el-option label="是" :value="true" />
            <el-option label="否" :value="false" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button class="cancelbtn" @click="cancel()">取 消</el-button>
        <el-button type="primary" class="savebtn" @click="save('ruleForm')">保 存</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import DataTypeManageApi from "@/api/basic-info-manage/data-type-manage";
import DataItemManageApi from "@/api/basic-info-manage/data-item-manage";
import DeviceManagementApi from "@/api/basic-info-manage/device-management";
import EnumApi from "@/api/enum";

export default {
  name: '',
  components: {},
  props: {
    // 弹框名称
    titleText: {
      type: String,
      default: ""
    },
    nowChanceStau: {
      type: String,
      default: ""
    },
    show: {
      type: Boolean,
      default: false
    },
    editObject: {
      type: [Object],
      default: () => {
        return {}
      }
    },
  },
  data() {
    return {
      formData: {
        dataType: {}
      },
      rules: {
        dataItemName: [
          { required: true, message: "请输入数据项名称", trigger: ["blur"] }
        ],
        dataTypeId: [
          { required: true, message: "选择数据类型", trigger: ["blur"] }
        ],
        equipmentId: [
          { required: true, message: "请选择设备", trigger: ["blur"] }
        ],
        visible: [
          { required: true, message: "请选择是否可展示", trigger: ["blur"] }
        ],
      },
      // 数据项类型
      dataTypeList: [],
      // 设备
      equipmentlist: [],
      showDialog: this.show,
    };
  },

  watch: {
    show(v) {
      this.showDialog = v;
    },
    nowChanceStau(v) {
      if (v === 'add') {
        this.formData = {
          dataType: {},
          staffName: sessionStorage.getItem('staffName'),
        }
      }
      if (v === 'edit') {
        this.formData = this.editObject
        this.$set(this.formData.dataType, 'mainingControlTypeValue', this.formData.dataType.mainingControlTypeStr)
      }
    },
  },
  created() {
    this.querydataType()
    this.queryEnum()
    this.queryDeviceManagement()
  },
  mounted() {
  },
  methods: {
    querydataType() {
      DataTypeManageApi.queryByCondition({ whetherAutomatic: false }).then(res => {
        this.dataTypeList = res.data
      })
    },
    queryEnum() {
      EnumApi.list({ enumType: 'MainingControlType' }).then(res => {
        this.enumList = res.data
      })
    },
    // 查询设备
    queryDeviceManagement() {
      DeviceManagementApi.queryByCondition().then(res => {
        this.equipmentlist = res.data
      })
    },
    changeDataType(e) {
      this.dataTypeList.forEach(item => {
        if (item.dataTypeId === e) {
          this.$set(this.formData.dataType, 'dataTypeName', item.dataTypeName)
          this.$set(this.formData.dataType, 'mainingControlTypeValue', item.mainingControlTypeStr)
          this.$set(this.formData.dataType, 'enumListType', item.enumListType)
          this.$set(this.formData.dataType, 'valueType', item.valueType)
        }
      });
    },
    save(formData) {
      this.$refs[formData].validate(valid => {
        if (valid) {
          if (this.nowChanceStau === "edit") {
            let params = { ...this.formData };
            DataItemManageApi.edit(params).then(res => {
              if (res.success) {
                this.$message.success("修改成功！");
                this.close()
              } else {
                // this.$message.error("系统错误,修改失败");
                this.$message.error(res.message);
              }
            });
          } else if (this.nowChanceStau === "add") {
            let params = { ...this.formData };
            params.whetherAutomatic = false;
            DataItemManageApi.add(params).then(res => {
              if (res.success) {
                this.$message.success("添加成功");
                this.close()
              } else {
                // this.$message.error("系统错误,添加失败");
                this.$message.error(res.message);
              }
            });
          }
        } else {
          return false;
        }
      });
    },
    close() {
      this.$emit("closeDialog");
      this.address = []
    },
    cancel() {
      this.$emit("cancelDialog");
      this.address = []
      this.$refs.ruleForm.resetFields()
    }
  }
};
</script>

<style lang="scss" scoped>
::v-deep .el-table th {
  color: #000;
  background-color: rgba(120, 182, 193, 0.4) !important;
}

::v-deep .el-table .el-table__row--striped {
  color: #000;
  background-color: rgba(186, 186, 186, 0.4) !important;
}

::v-deep.el-table td,
.el-table th.is-leaf,
.el-table--border,
.el-table--group,
.el-table th.is-leaf {
  border-color: #D3D3D3 !important;
}

::v-deep .el-table th.is-leaf {
  border-color: #D3D3D3 !important;
}

::v-deep .el-table__empty-text {
  color: #000;
}

::v-deep .el-table {
  color: #000;
}

.addInfo {
  width: 100%;
  margin: 10px;
  background-color: #237FC3;
  color: #fff;
  font-size: 18px;

}

.el-form {
  margin-top: 10px;
}
</style>
